package com.tianya.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianya.domain.dto.TeacherRequestDTO;
import com.tianya.domain.entity.Course;
import com.tianya.domain.entity.Teacher;
import com.tianya.domain.entity.User;
import com.tianya.domain.enums.AccountStatus;
import com.tianya.domain.enums.Message;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.domain.vo.TeacherVO;
import com.tianya.exception.CustomException;
import com.tianya.mapper.TeacherMapper;
import com.tianya.service.CourseService;
import com.tianya.service.TeacherService;
import com.tianya.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86191
* @description 针对表【teacher(教师表)】的数据库操作Service实现
* @createDate 2025-05-24 19:00:08
*/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
    implements TeacherService{
    @Resource
    private UserService userService;

    @Resource
    private CourseService courseService;
    @Override
    public void createTeacher(TeacherRequestDTO teacherRequestDTO) {
        //1.先往用户表里新增
        User user = new User();
        user.setName(teacherRequestDTO.getName());
        user.setUsername(teacherRequestDTO.getName());
        // 默认密码
        user.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        user.setRole(2L);
        user.setStatus(AccountStatus.NORMAL);
        userService.save(user);

        // 获取新增用户的ID
        Long userId = user.getUserId();
        Teacher teacher = new Teacher();
        teacher.setUserId(userId);
        teacher.setPosition(teacherRequestDTO.getPosition());
        teacher.setSubject(teacherRequestDTO.getSubject());

        save(teacher);
    }

    /**
     * 编辑教师信息
     * @param id
     * @param teacherRequestDTO
     */
    public void editTeacher(Long id, TeacherRequestDTO teacherRequestDTO) {
        Teacher teacher = getById(id);
        teacher.setPosition(teacherRequestDTO.getPosition());
        teacher.setSubject(teacherRequestDTO.getSubject());
        updateById(teacher);
    }

    @Override
    public TeacherVO getTeacherDetailVO(Long id) {
        Teacher teacher = getById(id);
        User user = userService.getById(teacher.getUserId());
        return TeacherVO.builder()
                .teacherId(teacher.getTeacherId())
                .name(user.getName())
                .gender(user.getGender())
                .phone(user.getPhone())
                .subject(teacher.getSubject())
                .position(teacher.getPosition())
                .createAt(teacher.getCreatedAt())
                .build();
    }

    @Override
    public PageResultVO<TeacherVO> pageList(Integer page, Integer size, TeacherRequestDTO teacherRequestDTO) {
        Page pageParam = new Page<>(page, size);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        if (teacherRequestDTO != null) {
            if (teacherRequestDTO.getSubject() != null) {
                queryWrapper.eq("subject", teacherRequestDTO.getSubject());
            }
            if (teacherRequestDTO.getPosition() != null) {
                queryWrapper.eq("position", teacherRequestDTO.getPosition());
            }
        }
        Page<Teacher> pageResult = baseMapper.selectPage(pageParam, queryWrapper);
        PageResultVO<TeacherVO> pageVO = new PageResultVO<>();
        pageVO.setTotal(pageResult.getTotal());
        List<TeacherVO> teacherVOList = new ArrayList<>();
        for (Teacher teacher : pageResult.getRecords()) {
            Long userId = teacher.getUserId();
            User user = userService.getById(userId);
            TeacherVO teacherVO = new TeacherVO();
            BeanUtil.copyProperties(user, teacherVO);
            BeanUtil.copyProperties(teacher, teacherVO);
            teacherVOList.add(teacherVO);
        }
        pageVO.setRecords(teacherVOList);
        return pageVO;
    }

    /**
     * 获取所有教师信息
     * @return
     */
    public List<TeacherVO> getAllTeachers() {
        List<Teacher> teachers = list();
        List<TeacherVO> teacherVOList = new ArrayList<>();
        for (Teacher teacher : teachers) {
            Long userId = teacher.getUserId();
            User user = userService.getById(userId);
            TeacherVO teacherVO = new TeacherVO();
            BeanUtil.copyProperties(teacher, teacherVO);
            if (user != null) {
                teacherVO.setName(user.getName());
                teacherVO.setPhone(user.getPhone());
                teacherVO.setGender(user.getGender());
            }
            teacherVOList.add(teacherVO);
        }
        return teacherVOList;
    }

    @Override
    public TeacherVO getTeacherByUserId(Long userId) {
        Teacher teacher = baseMapper.selectOne(new QueryWrapper<Teacher>().eq("user_id", userId));
        User user = userService.getById(teacher.getUserId());
        return TeacherVO.builder()
                .teacherId(teacher.getTeacherId())
                .name(user.getName())
                .gender(user.getGender())
                .phone(user.getPhone())
                .subject(teacher.getSubject())
                .position(teacher.getPosition())
                .createAt(teacher.getCreatedAt())
                .build();
    }
}




